package com.jds.test.bio.p6;

/**
 *
 * https://www.cnblogs.com/silyvin/articles/12019528.html
 * https://www.cnblogs.com/silyvin/articles/12037917.html
 * https://www.cnblogs.com/silyvin/articles/12041638.html
 * https://www.cnblogs.com/silyvin/articles/12057673.html
 * Created by joyce on 2019/11/26.
 */
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static final int PORT = 12123;
    public static final int BUFFER_SIZE = Client.BUFFER_SIZE;

    private static String flag = null;

    public static void main(String [] args) throws IOException, InterruptedException {

        /**
         * null 服务端不读任何字节
         * 1 sleep 12s 开始读
         */
        if (args != null && args.length > 0) {
            flag = args[0];
        }


        new Server().server();
    }

    //服务端代码
    public void server() throws IOException, InterruptedException{

     //   ServerSocket ss = new ServerSocket(PORT);
        ServerSocket ss = new ServerSocket();
        System.out.println(ss.getReceiveBufferSize());
        ss.setReceiveBufferSize(100);
        InetAddress bindAddr = null;
        ss.bind(new InetSocketAddress(bindAddr, PORT), 50);

        while(true) {
            Socket s = ss.accept();
            System.out.println(s.getRemoteSocketAddress().toString());

            // then do nothing, do not copy bytes from kernel
            if("12".equals(flag)) {
                Thread.sleep(12000);

                InputStream inputStream = s.getInputStream();
                byte [] bytes = new byte[BUFFER_SIZE];
                int i=0;
                while (inputStream.read(bytes) != -1) {
                    System.out.println(i++);
                }
            }
        }
    }
}